package com.heer.gymnasium.module.team.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.module.team.beans.TeamBean;
import com.heer.gymnasium.module.team.beans.TeamResponseBean;
import com.heer.gymnasium.util.ImageUtil;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.commonadapter.quick.Quick;
import com.zhihuiyun.library.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/2/11
 * 我的战队列表
 */

public class MyTeamListActivity extends BaseRefreshActivity {
    private static final int RES_ID_LAYOUT = R.layout.layout_team_list;
    private static final int RES_ID_CREATE_LABEL = R.id.layout_team_list_create_label;
    private static final int RES_ID_CREATE_PANEL = R.id.layout_team_list_create_panel;
    private static final int RES_ID_REFRESH = R.id.layout_team_list_refresh_srl;
    private static final int RES_ID_LIST = R.id.layout_team_list_lv;

    private static final int RES_ID_ITEM = R.layout.item_team;
    private static final int RES_ID_ITEM_ICON = R.id.item_team_icon_iv;
    private static final int RES_ID_ITEM_NAME = R.id.item_team_name_tv;
    private static final int RES_ID_ITEM_LINE = R.id.item_team_line_iv;
    private static final int RES_ID_ITEM_SIGN = R.id.item_team_sign_iv;
    private static final int RES_ID_ITEM_TYPE = R.id.item_team_type_tv;
    private static final int RES_ID_ITEM_NUM = R.id.item_team_peoplenum_tv;
    private static final int RES_ID_ITEM_CAPTAIN = R.id.item_team_captain_ll;
    private static final int RES_ID_ITEM_ENTER = R.id.item_team_enter_iv;


    private QuickTypeAdapter<TeamBean> adapter;
    private List<TeamBean> data;
    private Activity $this;
    private final int INTENT_CREATE = 0x11;
    private final int INTENT_DETAIL = 0x22;
    private boolean isFirst = true;

    public static void startActivity(Activity context) {
        Intent intent = new Intent(context, MyTeamListActivity.class);
        context.startActivity(intent);
    }

    @BindView(RES_ID_LIST)
    MyListView myListView;
    @BindView(RES_ID_CREATE_PANEL)
    View vCreateTeam;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_LAYOUT);
        setRefreshResId(RES_ID_REFRESH);

        Quick.init(new Quick.QuickLoad() {
            @Override
            public void load(Context context, String url, ImageView view) {
                ImageUtil.loadUrl(context, url, DRAWABLE_DEFAULT_LOGO, view);
            }
        });
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("我的战队");
        titleUtils.startMainActivity($this);
        data = new ArrayList<>();
        adapter = new QuickTypeAdapter<TeamBean>(this, data, RES_ID_ITEM) {
            @Override
            public void bindData4View(ViewHolder holder, final TeamBean data, int type, int pos) {
                holder.setText(RES_ID_ITEM_NAME, data.getTeamName());
                holder.setText(RES_ID_ITEM_NUM, data.getPeopleNum());
                holder.setImg(context, RES_ID_ITEM_ICON, data.getIconUrl());
                switch (data.getTeamType()) {
                    case 1:
                        holder.setText(RES_ID_ITEM_TYPE, "白金战队");
                        holder.setBg(RES_ID_ITEM_LINE, R.drawable.ic_platinum_line);
                        holder.setBg(RES_ID_ITEM_SIGN, R.drawable.ic_team_platinum);
                        break;
                    case 2:
                        holder.setText(RES_ID_ITEM_TYPE, "黄金战队");
                        holder.setBg(RES_ID_ITEM_LINE, R.drawable.ic_gold_line);
                        holder.setBg(RES_ID_ITEM_SIGN, R.drawable.ic_team_gold);
                        break;
                    case 3:
                        holder.setText(RES_ID_ITEM_TYPE, "白银战队");
                        holder.setBg(RES_ID_ITEM_LINE, R.drawable.ic_silver_line);
                        holder.setBg(RES_ID_ITEM_SIGN, R.drawable.ic_team_silver);
                        break;
                    default:
                        holder.setText(RES_ID_ITEM_TYPE, "青铜战队");
                        holder.setBg(RES_ID_ITEM_LINE, R.drawable.ic_bronze_line);
                        holder.setBg(RES_ID_ITEM_SIGN, R.drawable.ic_team_bronze);
                        break;
                }
                if (data.isContain()) {
                    holder.setVisibility(RES_ID_ITEM_CAPTAIN, View.VISIBLE);
                    holder.setVisibility(RES_ID_ITEM_ENTER, View.GONE);
                    vCreateTeam.setVisibility(View.GONE);
                } else {
                    holder.setVisibility(RES_ID_ITEM_CAPTAIN, View.GONE);
                    holder.setVisibility(RES_ID_ITEM_ENTER, View.VISIBLE);
                }

                holder.getParentView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TeamDetailActivity.startActivity($this, INTENT_DETAIL, data.getId());
                    }
                });
            }
        };
        myListView.setAdapter(adapter);

    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        getMyCorpsList(loadMore);
    }

    private void getMyCorpsList(final boolean loadMore) {
        mAppHelper.apiService().getMyCorpsList(String.valueOf(pageNo)).enqueue(new BaseCallback<ApiResponse<TeamResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<TeamResponseBean>> call, Response<ApiResponse<TeamResponseBean>> response) {
                if (!loadMore) {
                    data.clear();
                }
                layoutRefresh.setRefreshing(false);
                data.addAll(TeamBean.dataChange(response.body().getData().getData()));
                vCreateTeam.setVisibility(View.VISIBLE);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Call<ApiResponse<TeamResponseBean>> call, Throwable t) {
                super.onError(call, t);
                layoutRefresh.setRefreshing(false);
            }

            @Override
            public void onTokenTimeOut() {
                super.onTokenTimeOut();
                layoutRefresh.setRefreshing(false);
            }
        });
    }

    @OnClick(RES_ID_CREATE_LABEL)
    public void toCreateTeam() {
        CreateTeamActivity.startActivity(this, INTENT_CREATE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_CREATE && resultCode == RESULT_OK) {
            loadData(false);
        }

        if (requestCode == INTENT_DETAIL && resultCode == RESULT_OK) {
            loadData(false);
        }
    }
}
